{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Positional argument tuple: ()\n",
      "Positional argument tuple: (1, 2, '123')\n"
     ]
    }
   ],
   "source": [
    "def print_args(*args):\n",
    "    print('Positional argument tuple:',args)\n",
    "\n",
    "print_args()\n",
    "print_args(1,2,'123')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Keyword arguments: {'wine': 'merlot', 'entree': 'mutton'}\n"
     ]
    }
   ],
   "source": [
    "def print_kwargs(**kwargs):\n",
    "    print('Keyword arguments:',kwargs)\n",
    "\n",
    "print_kwargs(wine='merlot',entree='mutton')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n"
     ]
    }
   ],
   "source": [
    "def answer():\n",
    "    print(42)\n",
    "def run_something(func):\n",
    "    func()\n",
    "run_something(answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def outer(a,b):\n",
    "    def inner(c,d):\n",
    "        return c+d\n",
    "    return inner(a,b)\n",
    "outer(6,7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def knights2(saying):\n",
    "    def inner2():\n",
    "        return \"We are the knights who say : '%s'\" % saying\n",
    "    return inner2\n",
    "a=knights2('Duck')\n",
    "b=knights2('Hasenpfeffer')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "function"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We are the knights who say : 'Duck'\n",
      "We are the knights who say : 'Hasenpfeffer'\n"
     ]
    }
   ],
   "source": [
    "print(a())\n",
    "print(b())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thud!\n",
      "Meow!\n",
      "Thud!\n",
      "Hiss!\n"
     ]
    }
   ],
   "source": [
    "def edit_story(words,func):\n",
    "    for word in words:\n",
    "        print(func(word))\n",
    "stairs=['thud','meow','thud','hiss']\n",
    "\n",
    "edit_story(stairs,lambda word:word.capitalize()+'!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def document_it (func):\n",
    "    def new_function(*args,**kwargs):\n",
    "        print('Running function:',func.__name__)\n",
    "        print('Positional arguments:',args)\n",
    "        print('Keyword arguments:',kwargs)\n",
    "        result=func(*args,**kwargs)\n",
    "        print('Result:',result)\n",
    "        return result\n",
    "    return new_function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def add_ints(a,b):\n",
    "    return a+b\n",
    "add_ints(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running function: add_ints\n",
      "Positional arguments: (3, 5)\n",
      "Keyword arguments: {}\n",
      "Result: 8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cooler_add_ints=document_it(add_ints)\n",
    "cooler_add_ints(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running function: add_ints\n",
      "Positional arguments: (3, 5)\n",
      "Keyword arguments: {}\n",
      "Result: 8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@document_it\n",
    "def add_ints(a,b):\n",
    "    return a+b\n",
    "add_ints(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def square_it(func):\n",
    "    def new_function(*args,**kwargs):\n",
    "        result=func(*args,**kwargs)\n",
    "        return result*result\n",
    "    return new_function\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running function: new_function\n",
      "Positional arguments: (3, 5)\n",
      "Keyword arguments: {}\n",
      "Result: 64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@document_it\n",
    "@square_it\n",
    "def add_ints(a,b):\n",
    "    return a+b\n",
    "add_ints(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running function: add_ints\n",
      "Positional arguments: (3, 5)\n",
      "Keyword arguments: {}\n",
      "Result: 8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@square_it\n",
    "@document_it\n",
    "def add_ints(a,b):\n",
    "    return a+b\n",
    "add_ints(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
